package com.jilijili.controller;


import com.jilijili.config.mq.RabbitMQConfig;
import com.jilijili.grace.result.GraceJSONResult;
import com.jilijili.pojo.Comment;
import com.jilijili.service.CommentService;
import com.jilijili.utils.redis.RedisOperator;
import com.jilijili.utils.tencent.SMSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(value = "通用的测试接口", tags = "通用的测试接口", produces = "ljj", consumes = "app")
@RestController
@RequestMapping("/api/test")
@RefreshScope
public class BaseController {

    @Value("${nacos.counts}")
    public Integer nacosCounts = 123;

    @Value("${server.port}")
    public String port;

    @Resource
    private CommentService commentService;

    @Resource
    public RedisOperator redis;

    @Resource
    public RabbitTemplate rabbitTemplate;


    @ApiOperation(value = "测试端口")
    @GetMapping("/getPort")
    public GraceJSONResult getPort() {
        return GraceJSONResult.ok("hello " + this.port);
    }


    @ApiOperation(value = "RabbitMQ测试接口2")
    @GetMapping("/getValue")
    public GraceJSONResult getNacosConfig() {
        return GraceJSONResult.ok(this.nacosCounts);
    }


    @ApiOperation(value = "RabbitMQ测试接口2")
    @GetMapping("/produce2")
    public GraceJSONResult produce2() {
        this.rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_MSG,
                "routing.sys.msg.deleted",
                "这是一条测试消息deleted"
        );

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "RabbitMQ测试接口1")
    @GetMapping("/produce")
    public GraceJSONResult produce() {
        this.rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_MSG,
                "routing.sys.msg.send",
                "这是一条测试消息"
        );

        return GraceJSONResult.ok();
    }


    @ApiOperation(value = "Redis测试")
    @GetMapping("/testRedis")
    public GraceJSONResult testRedis() {

        return GraceJSONResult.ok();
    }

    @ApiImplicitParam(name = "phone", value = "手机号")
    @ApiOperation(value = "短信测试")
    @GetMapping("/sendMesage")
    public GraceJSONResult testMessages() {
        try {

            new SMSUtils().sendSMS("17754576486", "12345");

            return GraceJSONResult.ok("短信发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return GraceJSONResult.errorMsg("短信发送失败:" + e);
        }
    }

    @ApiImplicitParam(name = "测试用例", value = "参数说明")
    @ApiOperation(value = "功能用例")
    @GetMapping("/hello")
    public GraceJSONResult doHello() {
        List<Comment> list = commentService.list();
        return GraceJSONResult.ok(list);

    }
}
